import java.util.List;
//202 - 快乐数
class Solution {
    public boolean isHappy(int n) {
        List<Integer> list = new ArrayList<>();
        list.add(n);
        if(n == 0){
            return false;
        }
        int ans = sum(n);
        while(ans != 1){
            ans = sum(ans);
            if(list.contains(ans)){//判断有没有重复出现
                return false;
            }
            list.add(ans);
        }
        return true;
    }
    private int sum(int n){
        if(n == 0){
            return 0;
        }
        return (int)(sum(n/10) + Math.pow(n%10,2));
    }
}
//190 - 颠倒二进制位
public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        int a = 0;
        for(int i = 0; i < 32; i++){
            a |= (n&1) <<(31-i);
            n = n >>> 1;
        }
        return a;
    }
}
//258 - 各位相加
class Solution {
    public int addDigits(int num) {
        int sum = ans(num);
        while(sum > 9){
            sum = ans(sum);
        }
        return sum;
    }
    private int ans(int num){//得到一个数各位相加的值
        if(num == 0){
            return 0;
        }
        return ans(num/10) + num%10;
    }
}

class Solution {
    public int addDigits(int num) {
        int sum = ans(num);
        while(sum > 9){
            sum = ans(sum);
        }
        return sum;
    }
    private int ans(int num){
        int sum = 0;
        while(num != 0){
            sum += num%10;
            num /= 10;
        }
        return sum;
    }
}
//257 - 二叉树的所有路静
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<>();
        dfs(root, "", list);
        return list;
    }
    private void dfs(TreeNode root, String a, List<String> list){
        if(root != null){
            StringBuffer s = new StringBuffer(a);//将之前的路径保存下来
            s.append(String.valueOf(root.val));
            if(root.left == null && root.right == null ){
                list.add(s.toString());
            }else{
                s.append("->");
                dfs(root.left, s.toString(), list);
                dfs(root.right, s.toString(), list);
            }
        }
    }
}

public class Test {
}
